﻿// CBottomWnd.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "CBottomWnd.h"
#include "MainFrm.h"
#include "MaskWnd.h"

#define CTRL_ARALM_WIDTH  40 
#define CTRL_ARALM_COUNT  2 
#define CTRL_ARALM_COUNT_EPC  3 
// CBottomWnd

IMPLEMENT_DYNCREATE(CBottomWnd, CFormView)
CBottomWnd* bot_dlg;
CBottomWnd::CBottomWnd()
	: CFormView(IDD_CBottomWnd)
{
	m_nOldStatus[0] = m_nOldStatus[1] = m_nOldStatus[2] = m_nOldStatus[3] = m_nOldStatus[4] = m_nOldStatus[5] = 0;
#ifdef DEMO
	m_nDemoTimer = 0;
#endif
	m_strOldMsg=_T("");
	m_nErrorLast=0;
	m_nZeroFidLast=0;
	m_nFirNum = 0;
	m_bUpdateBottom=FALSE;
	m_lock_status = false;
}

CBottomWnd::~CBottomWnd()
{
}

void CBottomWnd::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BTRT, m_btRT);
	DDX_Control(pDX, IDC_EXIT, m_bexit);
	DDX_Control(pDX, IDC_LOCK, m_block);
	DDX_Control(pDX, IDC_BTHQ, m_btHQ);
	DDX_Control(pDX, IDC_BTMS, m_btMS);
	DDX_Control(pDX, IDC_BTMC, m_btMC);
	DDX_Control(pDX, IDC_BTSYSTEM, m_btSys);
}

BEGIN_MESSAGE_MAP(CBottomWnd, CFormView)
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_BTRT, &CBottomWnd::OnBnClickedBtrt)
	ON_BN_CLICKED(IDC_BTHQ, &CBottomWnd::OnBnClickedBthq)
	ON_BN_CLICKED(IDC_BTMS, &CBottomWnd::OnBnClickedBtms)
	ON_BN_CLICKED(IDC_BTMC, &CBottomWnd::OnBnClickedBtmc)
	ON_BN_CLICKED(IDC_BTSYSTEM, &CBottomWnd::OnBnClickedBtsystem)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_LOCK, &CBottomWnd::OnBnClickedLock)
	ON_BN_CLICKED(IDC_EXIT, &CBottomWnd::OnBnClickedExit)
END_MESSAGE_MAP()


// CBottomWnd 诊断

#ifdef _DEBUG
void CBottomWnd::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CBottomWnd::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CBottomWnd 消息处理程序


BOOL CBottomWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
	// TODO: 在此添加专用代码和/或调用基类

	return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}


void CBottomWnd::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	timeout.SetTime(15 * 1000);// 设定超时时间为5000毫秒 (5秒)
	bot_dlg = this;
	// TODO: 在此添加专用代码和/或调用基类
	m_btRT.SetIcon(IDI_MAIN1, NULL);
	m_btHQ.SetIcon(IDI_MAIN2, NULL);
	m_btMS.SetIcon(IDI_MAIN3, NULL);
	m_btMC.SetIcon(IDI_MAIN4, NULL);
	m_btSys.SetIcon(IDI_MAIN5, NULL);
	m_bexit.SetIcon(IDC_EXIT, NULL);
	m_block.SetIcon(IDC_UNLOCK, NULL);

	m_nTimer = SetTimer(9005, 1000, NULL);
#ifdef DEMO
	m_nDemoTimer = SetTimer(9010, 250, NULL);
	cfDemo = gl.ReadChmDate(m_strAppPath+_T("\\CH1_1203 163009.bcf"));
	gl.m_bConnChrom = TRUE;
#endif
}


void CBottomWnd::OnDraw(CDC* pSDC)
{
	// TODO: 在此添加专用代码和/或调用基类
	CRect rect;
	GetClientRect(rect);
	CBitmap*Oldmap;
	CBitmap cBitmap;
	CDC memDC;
	cBitmap.CreateCompatibleBitmap(pSDC, rect.Width(), rect.Height());
	memDC.CreateCompatibleDC(pSDC);
	Oldmap = memDC.SelectObject(&cBitmap);
	CDC* pDC = &memDC;
	OnPrepareDC(pDC);

	Draw(pDC);

	pSDC->SetViewportOrg(0, 0);
	pSDC->SetWindowOrg(0, 0);
	pSDC->SetMapMode(MM_TEXT);

	pDC->SetViewportOrg(0, 0);
	pDC->SetWindowOrg(0, 0);
	pDC->SetMapMode(MM_TEXT);

	CRect UpdateRect;
	pSDC->GetClipBox(UpdateRect);

	pSDC->BitBlt(UpdateRect.left, UpdateRect.top,
		rect.Width(), rect.Height(), pDC,
		UpdateRect.left, UpdateRect.top, SRCCOPY);
	memDC.SelectObject(Oldmap);
	memDC.DeleteDC();
}


void	CBottomWnd::Draw(CDC* pDC)
{
	CRect rect;
	GetClientRect(rect);
	BxGradientFill(pDC, rect, BACKCOLOR1, BACKCOLOR2, 1);
	CFont* pF = (CFont*)pDC->SelectObject(&gl.m_pSmallFont); //保存当前字体
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(RGB(255, 255, 255));
	CSize s = m_dibFid1.GetDimensions();
	s.cx /= CTRL_ARALM_COUNT;

	CSize sEPC = m_dibEPC.GetDimensions();
	sEPC.cx /= CTRL_ARALM_COUNT_EPC;

	CPoint pt1 = CPoint(rect.right-6* CTRL_ARALM_WIDTH*g_DPIScaleX, rect.top+5*g_DPIScaleY);
	CPoint dp;
	CRect rt = rect;

	dp.x = 0;
	if (gl.gcStatus.m_nFid1Status ==0)
		dp.x = s.cx;
	dp.y = 0;
	m_dibFid1.Draw(pDC, pt1, CSize(s.cx/1.7*g_DPIScaleX, s.cy/1.7*g_DPIScaleY), dp, s);
	m_rtUpdate[0] = CRect(pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY));
	rt = CRect(CPoint(pt1.x- s.cx/1.5*g_DPIScaleX, pt1.y + s.cy / 2*g_DPIScaleY), CSize(s.cx*2*g_DPIScaleX, s.cy / 2*g_DPIScaleY));
	pDC->DrawTextW(_T("组份A"), rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	pt1.x = pt1.x + CTRL_ARALM_WIDTH*g_DPIScaleX;
	dp.x = 0;
	if (gl.gcStatus.m_nFid2Status == 0)
		dp.x = s.cx;
	dp.y = 0;
	m_dibFid2.Draw(pDC, pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY), dp, s);
	m_rtUpdate[1] = CRect(pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY));
	rt = CRect(CPoint(pt1.x - s.cx / 1.5*g_DPIScaleX, pt1.y + s.cy / 2*g_DPIScaleY), CSize(s.cx * 2*g_DPIScaleX, s.cy / 2*g_DPIScaleY));
	pDC->DrawTextW(_T("组份B"), rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	pt1.x = pt1.x + CTRL_ARALM_WIDTH*g_DPIScaleX;
	dp.x = 0;
	if (gl.gcStatus.m_nFid3Status == 0)
		dp.x = s.cx;
	dp.y = 0;
	m_dibFid3.Draw(pDC, pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY), dp, s);
	m_rtUpdate[2] = CRect(pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY));
	rt = CRect(CPoint(pt1.x - s.cx / 1.5*g_DPIScaleX, pt1.y + s.cy / 2*g_DPIScaleY), CSize(s.cx * 2*g_DPIScaleX, s.cy / 2*g_DPIScaleY));
	pDC->DrawTextW(_T("全烃"), rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	pt1.x = pt1.x + CTRL_ARALM_WIDTH*g_DPIScaleX;
	dp.x = sEPC.cx * gl.gcStatus.m_nEPCStatus; // 0--正常，1--关闭， 2--压力有误差
	dp.y = 0;
	m_dibEPC.Draw(pDC, pt1, CSize(sEPC.cx / 1.7*g_DPIScaleX, sEPC.cy / 1.7*g_DPIScaleY), dp, sEPC);
	m_rtUpdate[3] = CRect(pt1, CSize(sEPC.cx / 1.7*g_DPIScaleX, sEPC.cy / 1.7*g_DPIScaleY));
	rt = CRect(CPoint(pt1.x - sEPC.cx / 1.5*g_DPIScaleX, pt1.y + sEPC.cy / 2*g_DPIScaleY), CSize(sEPC.cx * 2*g_DPIScaleX, sEPC.cy / 2*g_DPIScaleY));
	pDC->DrawTextW(_T("EPC"), rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	pt1.x = pt1.x + CTRL_ARALM_WIDTH*g_DPIScaleX;
	dp.x = 0;
	if (gl.ChromWits.nWitsOutStatus == 0)
		dp.x = s.cx;
	dp.y = 0;
	m_dibWitsOut.Draw(pDC, pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY), dp, s);
	m_rtUpdate[4] = CRect(pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY));
	rt = CRect(CPoint(pt1.x - s.cx / 1.5*g_DPIScaleX, pt1.y + s.cy / 2*g_DPIScaleY), CSize(s.cx * 2*g_DPIScaleX, s.cy / 2*g_DPIScaleY));
	pDC->DrawTextW(_T("输出"), rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	pt1.x = pt1.x + CTRL_ARALM_WIDTH*g_DPIScaleX;
	dp.x = 0;
	if (gl.ChromWits.nWitsInStatus == 0)
		dp.x = s.cx;
	dp.y = 0;
	m_dibWitsIn.Draw(pDC, pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY), dp, s);
	m_rtUpdate[5] = CRect(pt1, CSize(s.cx / 1.7*g_DPIScaleX, s.cy / 1.7*g_DPIScaleY));
	rt = CRect(CPoint(pt1.x - s.cx / 1.5*g_DPIScaleX, pt1.y + s.cy / 2*g_DPIScaleY), CSize(s.cx * 2*g_DPIScaleX, s.cy / 2*g_DPIScaleY));
	pDC->DrawTextW(_T("输入"), rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	pDC->SelectObject(pF);		//恢复以前的字体
}



int CBottomWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	m_dibFid1.LoadResource(IDB_FIRECH1);
	m_dibFid2.LoadResource(IDB_FIRECH2);
	m_dibFid3.LoadResource(IDB_FIRETG);
	m_dibEPC.LoadResource(IDB_TALARM);
	m_dibWitsIn.LoadResource(IDB_TALARM);
	m_dibWitsOut.LoadResource(IDB_TALARM);
	return 0;
}

//实时分析
void CBottomWnd::OnBnClickedBtrt()
{
	m_pParent->ChangeView(0);
}

//历史查询
void CBottomWnd::OnBnClickedBthq()
{
	m_pParent->ChangeView(1);
}

//仪器状态
void CBottomWnd::OnBnClickedBtms()
{
	m_pParent->ChangeView(2);
}

//设备标定
void CBottomWnd::OnBnClickedBtmc()
{
	m_pParent->ChangeView(3);
}

//系统设置
void CBottomWnd::OnBnClickedBtsystem()
{
	m_pParent->ChangeView(4);
}

UINT Fid_Fire_Again(LPVOID pParam)
{
	CMainFrame* pParent=(CMainFrame*)pParam;
	if(pParent->m_bIsFidThreadRunning==false){
		pParent->m_bIsFidThreadRunning = true;
	}else{
		return 0;
	}
	cControl.SetEPCPars();
	if (gl.gcStatus.m_nFid1Status==0){//组份A灭火,单独点火
		std::cout << "组份A灭火,单独点火" << std::endl;
		cControl.OnlySendFireCommand(1, gl.FiredEnabled(1));
		Sleep(5000);
	}
	if (gl.gcStatus.m_nFid2Status==0){//组份B灭火,单独点火
		std::cout << "组份B灭火,单独点火" << std::endl;
		cControl.OnlySendFireCommand(2, gl.FiredEnabled(2));
		Sleep(5000);
	}		
	if (gl.gcStatus.m_nFid3Status==0 ){//全烃灭火,单独点火
		std::cout << "全烃灭火,单独点火" << std::endl;
		cControl.OnlySendFireCommand(0, gl.FiredEnabled(0));
		Sleep(5000);
	}	

	//启动分析
	pParent->m_RealTimeView.ReStartRun();
	pParent->m_bIsFidThreadRunning = false;
	return 1;
}

void CBottomWnd::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_nTimer == nIDEvent)
	{
		if(!m_bUpdateBottom)
		{
			CRect rect;
			GetClientRect(&rect);
			InvalidateRect(rect,TRUE);
			m_bUpdateBottom=TRUE;
		}
		gl.m_strNewMsg1=_T("");
		bool bNeedAlarm = FALSE;
		for (int i = 0; i < 7; i++)
		{
			bool bNeedUpdate = FALSE;
			switch (i)
			{
			case 0:
				if (m_nOldStatus[0] != gl.gcStatus.m_nFid1Status)
				{
					m_nOldStatus[0] = gl.gcStatus.m_nFid1Status;
					bNeedUpdate = TRUE;
					if (gl.gcStatus.m_nFid1Status == 1) {
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("组份A鉴定器点火成功！");
						std::cout << "组份A鉴定器点火成功！" << std::endl;
					}
					else {
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("组份A鉴定器熄火，请检查！");
						std::cout << "组份A鉴定器熄火，请检查！" << std::endl;
					}
				}
				else if (gl.gcStatus.m_nFid1Status == 0 && timeout.IsTimeout()) {
					bNeedUpdate = TRUE; 
					gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("组份A鉴定器熄火，请检查！");
					std::cout << "组份A鉴定器熄火，请检查！2" << std::endl;
				}
				break;
			case 1:
				if (m_nOldStatus[1] != gl.gcStatus.m_nFid2Status)
				{
					m_nOldStatus[1] = gl.gcStatus.m_nFid2Status;
					bNeedUpdate = TRUE;
					if (gl.gcStatus.m_nFid2Status==1){
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("组份B鉴定器点火成功！");
						std::cout << "组份B鉴定器点火成功！" << std::endl;
					}
					else{
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("组份B鉴定器熄火，请检查！");
						std::cout << "组份B鉴定器熄火，请检查！" << std::endl;
					}
				}
				else if (gl.gcStatus.m_nFid2Status == 0 && timeout.IsTimeout()) {
					bNeedUpdate = TRUE; 
					gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("组份B鉴定器熄火，请检查！");
					std::cout << "组份B鉴定器熄火，请检查！2" << std::endl;
				}
				break;
			case 2:
				if (m_nOldStatus[2] != gl.gcStatus.m_nFid3Status)
				{
					m_nOldStatus[2] = gl.gcStatus.m_nFid3Status;
					bNeedUpdate = TRUE;
					if (gl.gcStatus.m_nFid3Status==1){
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("全烃鉴定器点火成功！");
						std::cout << "全烃鉴定器点火成功！" << std::endl;
					}
					else{
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("全烃鉴定器熄火，请检查！");
						std::cout << "全烃鉴定器熄火，请检查！" << std::endl;
					}
				}
				else if (gl.gcStatus.m_nFid3Status == 0 && timeout.IsTimeout()) {
					bNeedUpdate = TRUE; 
					gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("全烃鉴定器熄火，请检查！");
					std::cout << "全烃鉴定器熄火，请检查！2" << std::endl;
				}
				break;
			case 3:
				if (m_nOldStatus[3] != gl.gcStatus.m_nEPCStatus)
				{
					m_nOldStatus[3] = gl.gcStatus.m_nEPCStatus;
					bNeedUpdate = TRUE;
					if (gl.gcStatus.m_nEPCStatus == 0){
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("EPC模块通信正常！");
						std::cout << "EPC模块通信正常！" << std::endl;
					}
					else{
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("EPC模块通信异常，请检查！");
						std::cout << "EPC模块通信异常，请检查！" << std::endl;
					}
				}
				else if (gl.gcStatus.m_nEPCStatus != 0 && timeout.IsTimeout()) {
					bNeedUpdate = TRUE; 
					gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("EPC模块通信异常，请检查！");
					std::cout << "EPC模块通信异常，请检查！2" << std::endl;
				}
				break;
			case 4:
				if (m_nOldStatus[4] != gl.ChromWits.nWitsOutStatus)
				{
					m_nOldStatus[4] = gl.ChromWits.nWitsOutStatus;
					bNeedUpdate = TRUE;
					if (gl.ChromWits.nWitsOutStatus){
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("WITS输出端口开放正常！");
						std::cout << "WITS输出端口开放正常！" << std::endl;
					}
					else{
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("WITS输出端口异常，请检查！");
						std::cout << "WITS输出端口异常，请检查！" << std::endl;
					}
				}
				else if (gl.ChromWits.nWitsOutStatus == 0 && timeout.IsTimeout() && gl.ChromWits.nOutAutoOpen == 1) {
					bNeedUpdate = TRUE; 
					gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("WITS输出端口异常，请检查！");
					std::cout << "WITS输出端口异常，请检查！2" << std::endl;
				}
				break;
			case 5:
				if (m_nOldStatus[5] != gl.ChromWits.nWitsInStatus)
				{
					m_nOldStatus[5] = gl.ChromWits.nWitsInStatus;
					bNeedUpdate = TRUE;
					if (gl.ChromWits.nWitsInStatus){
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("WITS输入通信正常！");
						std::cout << "WITS输入通信正常！" << std::endl;
					}
					else{
						gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("WITS输入通信异常，请检查！");
						std::cout << "WITS输入通信异常，请检查！" << std::endl;
					}
				}
				else if (gl.ChromWits.nWitsInStatus == 0 && timeout.IsTimeout() && gl.ChromWits.nInAutoOpen ==1) {
					bNeedUpdate = TRUE;
					gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("WITS输入通信异常，请检查！");
					std::cout << "WITS输入通信异常，请检查！2" << std::endl;
				}
				break;
			case 6:
				{
					m_nFirNum++;	
					if(m_nFirNum>=30){
						m_nFirNum = 0;
												//gl.m_FireNumC = 0;
						//gl.m_FireNumB = 0;
						//gl.m_FireNumA = 0;
						if(gl.m_FireNumC >= gl.fire_num && !gl.gcStatus.m_nFid3Status){
							bNeedUpdate = TRUE;
							gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("全烃鉴定器点火状态异常，请检查！");
							std::cout << "全烃鉴定器点火状态异常，请检查！" << std::endl;
						}
						if(gl.m_FireNumA >= gl.fire_num && !gl.gcStatus.m_nFid1Status){
							bNeedUpdate = TRUE;
							gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("组份A鉴定器点火状态异常，请检查！");
							std::cout << "组份A鉴定器点火状态异常，请检查！" << std::endl;
						}
						if(gl.m_FireNumB >= gl.fire_num && !gl.gcStatus.m_nFid2Status){
							bNeedUpdate = TRUE;
							gl.m_strNewMsg1 = gl.m_strNewMsg1 + _T("组份B鉴定器点火状态异常，请检查！");
							std::cout << "组份B鉴定器点火状态异常，请检查！" << std::endl;
						}
					}
				}
				break;
			}
			if (bNeedUpdate)
			{
				bNeedAlarm=TRUE;
				InvalidateRect(m_rtUpdate[i]);
			}

			if(bNeedAlarm)
			{
				m_nErrorLast++;
				if(m_nErrorLast>20)
				{
					m_strOldMsg=_T("");
					m_nErrorLast=0;
				}
				gl.m_strNewMsg1.Trim();
				if (gl.m_strNewMsg1.GetLength()>0 && gl.m_strNewMsg1 !=m_strOldMsg)
				{
					m_strOldMsg=gl.m_strNewMsg1;
					AfxBeginThread(Speech_Info1, NULL, THREAD_PRIORITY_NORMAL);
				}
				if (gl.m_strNewMsg1.GetLength()==0) m_strOldMsg=gl.m_strNewMsg1;
			}
			else
			{
				m_strOldMsg=_T("");
				m_nErrorLast=0;
			}

			//判断是否要重新点火
			if (gl.gcStatus.m_nFid3Status==0 || gl.gcStatus.m_nFid1Status==0 || gl.gcStatus.m_nFid2Status==0)
				m_nZeroFidLast++;
			else
				m_nZeroFidLast=0;

			if (gl.gcStatus.m_nFid3Status >0 && gl.gcStatus.m_nFid1Status>0 && gl.gcStatus.m_nFid2Status>0)
			{
				gl.fire_status = true;
			}

			if(m_nZeroFidLast>60)
			{
				m_nZeroFidLast=0;
#ifdef QC_CNNMACHINE
					if (gl.fire_status == true && m_pParent->m_bIsFidThreadRunning == false){
						AfxBeginThread(Fid_Fire_Again, m_pParent, THREAD_PRIORITY_NORMAL);
					}
#endif
			}
		}

		//判断网络是否中断 5秒无数据返回时，自动重连
		if (CTime::GetCurrentTime().GetTime() > gl.m_nReceiveAD + 5 || CTime::GetCurrentTime().GetTime() > gl.m_nReceiveBD + 5)
		{
			WriteLog(_T("色谱控制板通信异常，正在重新连接………………"));
#ifdef SOCKET_MSG
			cControl.Close();
#else
			if(socketCb!=NULL)
				socketCb->Close();
			socketCb = NULL;
#endif
			gl.m_nReceiveBD =gl.m_nReceiveAD = CTime::GetCurrentTime().GetTime();
			ConnectQCBoard(false);//连接网络
			Sleep(1000);
			//启动分析
			m_pParent->m_RealTimeView.ReStartRun();
		}
	}

#ifdef DEMO
	if (m_nDemoTimer == nIDEvent)
	{

		int nCount = 10;
		long lRD[10];
		double dSum = 0;
		//A通道--组份
		for (int i = 0; i < nCount; i++)
		{
			if (gl.curChromA.CAIndex > MAX_CHROMSAMPLES - 10) gl.curChromA.CAIndex = 0;
			gl.curChromA.CA[gl.curChromA.CAIndex] = cfDemo.CA[gl.curChromA.CAIndex];
			gl.curChromA.CAIndex++;
			gl.gcStatus.dFidAV = cfDemo.CA[gl.curChromA.CAIndex];
		}
		//B通道--组份
		for (int i = 0; i < nCount; i++)
		{
			if (gl.curChromB.CAIndex > MAX_CHROMSAMPLES - 10) gl.curChromB.CAIndex = 0;
			gl.curChromB.CA[gl.curChromB.CAIndex] = cfDemo.CA[gl.curChromB.CAIndex];
			gl.curChromB.CAIndex++;
			gl.gcStatus.dFidBV = cfDemo.CA[gl.curChromB.CAIndex];;
		}
		//C通道--全烃
		dSum = 0;
		for (int i = 0; i < nCount; i++)
		{
			if (gl.curChromA.CBIndex > MAX_CHROMSAMPLES - 10) gl.curChromA.CBIndex = 0;
			if (gl.curChromB.CBIndex > MAX_CHROMSAMPLES - 10) gl.curChromB.CBIndex = 0;
			double dSignal = cfDemo.CB[gl.curChromB.CAIndex];		//转换成0-10V
			gl.curChromB.CB[gl.curChromB.CBIndex] = gl.curChromA.CB[gl.curChromA.CBIndex] = dSignal;
			dSum = dSum + dSignal;
			gl.curChromA.CBIndex++;
			gl.curChromB.CBIndex++;
		}
		if (nCount > 0)
			gl.gcStatus.dFidCV = dSum / nCount;
	}
#endif
	CFormView::OnTimer(nIDEvent);
}

void	CBottomWnd::ConnectQCBoard(bool bMsg)
{
	CString strIP = ReadINI(_T("Setting"), _T("IP"));
#ifdef QC_CNNMACHINE
	#ifdef SOCKET_MSG
		cControl.InitAndConnectNetGatherCard(AfxGetMainWnd()->m_hWnd, 2000, strIP);
	#else
		socketCb = new CCbSocket((CMainFrame *)AfxGetMainWnd());
		socketCb->Create();
		if (!socketCb->Connect(strIP, 2000))
			if(bMsg) AfxMessageBox(_T("不能与色谱仪建立网络连接！"), MB_ICONINFORMATION);
		cControl.QueryMachineInfo();
	#endif
#endif
}


void CBottomWnd::OnBnClickedLock()
{
	if(!m_lock_status){//当前锁定，
		//当前解锁
		//开始锁定
		m_block.SetIcon(IDC_LOCK, NULL);
		m_lock_status = true;
		CMaskWnd maskWnd;
		maskWnd.DoModal();
		m_block.SetIcon(IDC_UNLOCK, NULL);//开始解锁
		m_lock_status = false;
	}
}


void CBottomWnd::OnBnClickedExit()
{
  // 更改键盘类型并显示它   

    // 更改所需键盘的注册表项
			if (AfxMessageBox(_T("请确认退出 双通道快速色谱工作站软件？"), MB_YESNO | MB_ICONSTOP) == IDYES)
		{
			//关闭EPC
			cControl.CloseEPC();
			Sleep(1000);

			CString strFileName;
			strFileName.Format(_T("%s\\SystemMethod.mtd"), m_strAppPath);
			gl.SaveCalMethod(strFileName);

			gl.SaveSoftwareSetting();

			if (m_pCnnWell->GetState() == 1)
			{
				m_pCnnWell->Close();
				m_pCnnWell = NULL;
			}
			WriteLog(_T("双通道色谱软件关闭******"));
			exit(0);
		}
}